import FPJDefine from "../Game/FPJ/Prefabs/FPJDefine";
import {UIMgr} from "../lib/UIMgr";
import {glConfig} from "../Config/GLConfig";

const {ccclass, property} = cc._decorator;

@ccclass
export default class BtnVipRoom extends cc.Component {

    @property(cc.Sprite)
    icon: cc.Sprite = null;

    @property(cc.SpriteFrame)
    vipSpF:cc.SpriteFrame = null;

    @property(cc.SpriteFrame)
    hallSpF:cc.SpriteFrame = null;
    @property(cc.Node)
    BtnFriendList:cc.Node = null;
    @property(cc.Node)
    BtnDetailChat:cc.Node = null;

    _lock = false;

    start () {
        cc.systemEvent.on("EnterTable",this.initUI.bind(this) );
    }

    onDestroy(){
        cc.systemEvent.off("EnterTable",this.initUI );
    }

    initUI(){
        this.icon.spriteFrame = FPJDefine.isVipRoom() ? this.hallSpF : this.vipSpF;
        this.BtnFriendList.active = FPJDefine.isVipRoom();
        this.BtnDetailChat.active = FPJDefine.isVipRoom();
    }

    onEnable(){
        setTimeout(()=>{
            this.initUI();
        },10)
    }

    btnClick(){
        cc.vv.audioUtil.playSound("/Hall/sound/button_onclicked");
        if(FPJDefine.isVipRoom() ){
            this.exitVipRoom();
        }else {

            let kouLing = cc.sys.localStorage.getItem("KouLing" );
            if(kouLing && kouLing.length > 0){
                //直接用口令进入游戏
                let enterVipData = {password:kouLing};
                cc.vv.GLUtils.sendRequest("ATTEnterPrivateRoom", enterVipData, (error, data) => {
                    if (!data.errorcode) {
                        FPJDefine.resetAllData();
                        FPJDefine.room_list = data.room_list; //房间列表
                        FPJDefine.player_list = data.player_list;// 玩家列表
                        FPJDefine.initAllTable();
                        FPJDefine.checkVistor()
                        this.enterVipRoom();
                    } else {
                        cc.sys.localStorage.setItem("KouLing","" );
                        cc.vv.GLUtils.showTips(data.errordes);
                        UIMgr.open(glConfig.DWC_KouLing_path,null,null,()=>{
                            this.enterVipRoom();
                        })
                    }
                });
            }else {
                UIMgr.open(glConfig.DWC_KouLing_path,null,null,()=>{
                    this.enterVipRoom();
                })
            }
        }
    }

    setLock(){
        this._lock = true;
    }
    enterVipRoom(){
        if(this._lock){return}

        this.initUI();
        //其他
        cc.vv.eventCustom.emit("EnterVipRoom",true);
        this.setLock();
        //刷新大厅桌子
        cc.vv.eventCustom.emit("refreshHallTable");
        this._lock = false;
    }

    exitVipRoom(){
        if(this._lock){return}

        cc.vv.GLUtils.sendRequest("ATTLeavePrivateRoom", {}, (error, data) => {
            if (!data.errorcode) {
                FPJDefine.resetAllData();
                FPJDefine.room_list = data.room_list; //房间列表
                FPJDefine.player_list = data.player_list;// 玩家列表
                FPJDefine.initAllTable();
                FPJDefine.checkVistor()

                this.initUI();
                //
                cc.vv.eventCustom.emit("EnterVipRoom",false);
                this.setLock();
                //刷新大厅桌子
                cc.vv.eventCustom.emit("refreshHallTable");
                this._lock = false;

            } else {
                cc.vv.GLUtils.showTips(data.errordes);
            }
        });


    }
}
